<?php
/**
 *+------------------
 * Lflow
 *+------------------
 * Copyright (c) 2023~2030 gitee.com/liu_guan_qing All rights reserved.本版权不可删除，侵权必究
 *+------------------
 * Author: Mr.April(405784684@qq.com)
 *+------------------
 */

namespace app\adminapi\controller\v1\wf;

use app\adminapi\controller\AuthController;
use app\Request;
use lflow\core\services\CategoryServices;
use think\App;

class Category extends AuthController
{

    protected CategoryServices $service;

    public function __construct(App $app, CategoryServices $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 资源列表
     *
     * @return \think\response\Json
     * @throws \ReflectionException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(): \think\response\Json
    {
        $where = $this->request->getMore([
            ['id', ''],
            ['name', ''],
        ]);
        return app('json')->success($this->service->select($where));
    }

    /**
     * 获取指定资源
     *
     * @param string $id
     *
     * @return \think\Response\Json
     */
    public function read(string $id): \think\Response\Json
    {
        $ret = $this->service->find($id);
        if ($ret) {
            return app('json')->success($ret->toArray());
        } else {
            return app('json')->fail('参数错误');
        }
    }

    /**
     * 添加资源
     *
     * @param \app\Request $request
     *
     * @return \think\Response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request): \think\Response\Json
    {
        $data = $this->request->postMore([
            ['name', ''],
            ['remarks', ''],
            ['sort', 10],
            ['user_id', $request->adminId()],
        ]);
        if (empty($data['name'])) {
            return app('json')->fail('名称不能为空');
        }
        if ($this->service->save($data)) {
            return app('json')->success(100021);
        } else {
            return app('json')->fail(100022);
        }
    }

    /**
     * 删除指定资源
     *
     * @param string $id
     *
     * @return \think\Response\Json
     */
    public function delete(string $id): \think\Response\Json
    {
        if (!$id) return app('json')->fail(100100);
        if (!($this->service->del($id))) {
            return app('json')->fail(100008);
        }
        return app('json')->success('删除成功');
    }

    /**
     * 批量删除资源
     *
     * @return \think\Response\Json
     */
    public function batchRemove(): \think\Response\Json
    {
        $ids = $this->request->post('data');
        if (empty($ids)) return app('json')->fail(100100);
        if (!($this->service->batchRemove($ids))) {
            return app('json')->fail(100008);
        }
        return app('json')->success('删除成功');
    }

    /**
     * 更新指定资源
     *
     * @param \app\Request $request
     * @param string       $id
     *
     * @return \think\Response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update(Request $request, string $id): \think\Response\Json
    {
        if (!$id || !($this->service->get($id))) {
            return app('json')->fail(100026);
        }
        $data = $this->request->postMore([
            ['name'],
            ['remark'],
            ['sort', 10],
            ['user_id', $request->adminId()],
        ]);
        if ($this->service->update($id, $data)) {
            return app('json')->success(100001);
        } else {
            return app('json')->fail(100007);
        }
    }
}
